package gatech.cs3251.project4;

import android.util.Log;

public class LocationFormat {
	public static final int size = 40;
	
	public int latitude;
	public int longitude;
	public int altitude;
	public int userId;
	
	public float f_latitude;
	public float f_longitude;
	public float f_altitude;
	
	public LocationFormat(int latitude, int longitude, int altitude, int userId) {
		this.latitude = latitude;
		this.longitude = longitude;
		this.altitude = altitude;
		this.userId = userId;
	}
	
	public LocationFormat(float latitude, float longitude, float altitude, int userId) {
		this.f_latitude = latitude;
		this.f_longitude = longitude;
		this.f_altitude = altitude;
		this.userId = userId;
	}
	
	public LocationFormat() {
		this(0,0,0,0);
	}
	
	public float getLatitude() {
		return Float.intBitsToFloat(latitude);
	}
	
	public float getLongitude() {
		return Float.intBitsToFloat(longitude);
	}
	
	public float getAltitude() {
		return Float.intBitsToFloat(altitude);
	}
	
	public float getLatitudeF()
	{
		return f_latitude;
	}
	public float getLongitudeF()
	{
		return f_longitude;
	}
	public float getAltitudeF()
	{
		return f_altitude;
	}
	
	public void setAltitude(float altitude) {
		altitude = Float.floatToIntBits(altitude);
	}
	
	public void setLongitude(float longitude) {
		altitude = Float.floatToIntBits(longitude);
	}
	
	public void setLatitude(float latitude) {
		altitude = Float.floatToIntBits(latitude);
	}
	
	/**
	 * Covert an integer into a network order String
	 * @param integer Integer to convert
	 * @return Integer in network order String
	 */
	public static String iToS(int integer) {
		String string = "";
		string += (char)(integer>>24)&0xff;
		string += (char)(integer>>16)&0xff;
		string += (char)(integer>>8)&0xff;
		string += (char)(integer)&0xff;
		
		return string;
	}
	
	/**
	 * Convert a network order integer inside a String to a string integer
	 * @param s String containing the network order integer
	 * @return The contained integer
	 */
	public static int sToI(String s) {
		int i=0;
		i += s.charAt(0)<<24;
		i += s.charAt(1)<<16;
		i += s.charAt(2)<<8;
		i += s.charAt(3);
		
		return i;
	}
	
	public static float sToF(String s)
	{
		float f;
		Log.v("CHRIS", "String in sToF: " + s);
		f = Float.parseFloat(s);
		Log.v("CHRIS", "Return from sToF: " + f);
		return f;
	}
	/**
	 * Parses a String into a Location
	 * @param s A String containing a Location
	 * @return A Location parsed from the String
	 */
	public static LocationFormat parseLocation(String s) {
		LocationFormat l = new LocationFormat(0,0,0,0);
		
		if(s.length() < 20) return null;
		
		l.latitude = sToI(s.substring(0, 4));
		l.longitude = sToI(s.substring(4, 8));
		l.altitude = sToI(s.substring(8, 12));
		l.userId = sToI(s.substring(12, 16));
		
		return l;
	}
	
	@Override
	public String toString() {
		return"";	
	}
	
	public String networkPrint() {
		String location = "";
		location += iToS(latitude);
		location += iToS(longitude);
		location += iToS(altitude);
		location += iToS(userId);
		location += iToS(0);
		
		return location;
	}
}
